﻿using System;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Compilation;
using System.Web.UI;
using CookComputing.XmlRpc;
using System.IO;

[XmlRpcService(
    Name = "BlogKickstart CS4790",
    Description = "XML RPC implementation of MetaWebLog API",
    AutoDocumentation = true)]
//Change this to your endpoint URL
[XmlRpcUrl("http://localhost:56104/livewriter")]


public class MetaBlogApi : XmlRpcService
{
    private static void CheckLoggedIn(string username, string password)
    {
        bool loggedin = true;
        if (!loggedin)
            throw new System.Security.Authentication
                .InvalidCredentialException("Access denied");
    }
    [XmlRpcMethod("blogger.getUsersBlogs")]
    public XmlRpcStruct[] getUsersBlogs(string appKey, string username,
        string password)
    {
        //CheckLoggedIn(username, password);
        //Uses xslt transformation to retrive recent posts
        XmlRpcStruct[] posts = new XmlRpcStruct[1];
        XmlRpcStruct rpcstruct = new XmlRpcStruct();

        //if you have more than one blog then loop them here
        rpcstruct.Add("blogid", "1");
        rpcstruct.Add("blogName", "BlogKickstart Blog");
        rpcstruct.Add("url", "http://localhost:56104/blogpost"); // Blog URL
        posts[0] = rpcstruct;
        return posts;
    }
    [XmlRpcMethod("metaWeblog.setTemplate")]
    public bool setTemplate(string appKey, string blogid, string username,
        string password, string template, string templateType)
    {
        //WINDOWS LIVE WRITER DOESN"T USE THIS YET
        return true;
    }
    [XmlRpcMethod("metaWeblog.getCategories")]
    public XmlRpcStruct[] getCategories(string blogid, string username,
        string password)
    {
        //CheckLoggedIn(username, password);

        int intCatCount = 1;
        XmlRpcStruct[] posts = new XmlRpcStruct[intCatCount];

        //START LOOP
        XmlRpcStruct rpcstruct = new XmlRpcStruct();
        rpcstruct.Add("categoryid", "1");
        rpcstruct.Add("title", "Blog category");
        rpcstruct.Add("description", "My blog category");
        posts[0] = rpcstruct;
        //END LOOP

        return posts;
    }
    [XmlRpcMethod("metaWeblog.getRecentPosts")]
    public XmlRpcStruct[] getRecentPosts(string blogid, string username,
        string password, int numberOfPosts)
    {
        //CheckLoggedIn(username, password);
        //RETRIEVE YOUR BLOG POSTS AND LOOP THROUGH THE RETURN BELOW
        int intBlogCount = 1;
        XmlRpcStruct[] posts = new XmlRpcStruct[intBlogCount];
        int i = 0;

        //Populate structure with posts
        //START LOOP

        XmlRpcStruct rpcstruct = new XmlRpcStruct();
        rpcstruct.Add("title", "My Blog Post");
        rpcstruct.Add("link", "http://myblog/blog01");
        rpcstruct.Add("description", "My blog content");
        rpcstruct.Add("pubDate", "01/01/2009");
        rpcstruct.Add("guid", "0001");
        rpcstruct.Add("postid", "1");
        rpcstruct.Add("keywords", "123 123");
        rpcstruct.Add("author", "Doug");
        posts[i] = rpcstruct;
        //END LOOP
        return posts;
    }
    [XmlRpcMethod("metaWeblog.getTemplate")]
    public string getTemplate(string appKey, string blogid,
        string username, string password, string templateType)
    {
        //CheckLoggedIn(username, password);
        // LIVE WRITER DOESNT USE THIS YET
        string strTemplate = string.Empty;
        return strTemplate;
    }

    [XmlRpcMethod("metaWeblog.newPost")]
    public string newPost(string blogid, string username,
        string password, XmlRpcStruct rpcstruct, bool publish)
    {
        //CheckLoggedIn(username, password);
        string newBlogID = "1";
        string blogTitle = rpcstruct["title"].ToString();
        string blogLink = rpcstruct["link"].ToString();
        string blogContent = rpcstruct["description"].ToString();
        string blogAuthor = rpcstruct["author"].ToString();
        string[] blogCategories = (string[])rpcstruct["categories"];

        return newBlogID;
    }

    [XmlRpcMethod("metaWeblog.editPost")]
    public bool editPost(string postid, string username, string password,
        XmlRpcStruct rpcstruct, bool publish)
    {
        //CheckLoggedIn(username, password);
        string blogTitle = rpcstruct["title"].ToString();
        string blogLink = rpcstruct["link"].ToString();
        string blogContent = rpcstruct["description"].ToString();
        string blogAuthor = rpcstruct["author"].ToString();
        string[] blogCategories = (string[])rpcstruct["categories"];

        return true;
    }

    [XmlRpcMethod("metaWeblog.getPost")]
    public XmlRpcStruct getPost(string postid, string username,
        string password)
    {
        //CheckLoggedIn(username, password);
        XmlRpcStruct rpcstruct = new XmlRpcStruct();
        rpcstruct.Add("title", "My blog title");
        rpcstruct.Add("link", "http://localhost:56104/blogpost");
        rpcstruct.Add("description", "My blog post content");
        rpcstruct.Add("pubDate", "01/01/2009");
        rpcstruct.Add("guid", "00001");
        rpcstruct.Add("postid", "1");
        rpcstruct.Add("author", "Doug");
        rpcstruct.Add("publish", "1");
        return rpcstruct;
    }

    [XmlRpcMethod("blogger.deletePost")]
    public bool deletePost(string appKey, string postid, string username,
        string password, bool publish)
    {
        //CheckLoggedIn(username, password);
        ///DELETE THE POST WITH THE ID 'postid'
        return false;
    }

    [XmlRpcMethod("metaWeblog.newMediaObject")]
    public XmlRpcStruct newMediaObject(string blogid, string username,
        string password, XmlRpcStruct rpcstruct)
    {
        //CheckLoggedIn(username, password);

        string name = rpcstruct["name"].ToString(); //file name
        string type = rpcstruct["type"].ToString(); //file type
        byte[] media = (byte[])rpcstruct["bits"];   //file contents
        FileStream stream = File.Create(name);
        stream.Write(media, 0, media.Length);
        stream.Flush();
        stream.Close();
        stream.Dispose();
        XmlRpcStruct rstruct = new XmlRpcStruct();
        rstruct.Add("url", "/new filename path/");
        return rstruct;
    }
}


